
const express = require("express");
const route = express.Router();

const advModel = require("../db/model/advModel")
const path = require("path")

const formidable = require("formidable")

const isLoginMiddle = require("../middleware/isLoginMiddle");






//添加广告 接口
route.post("/addAdv", isLoginMiddle, (req, res) => {

  const form = formidable({
    uploadDir: path.resolve(__dirname, "../upload"),
    keepExtensions: true
  })
  form.parse(req, (err, params, file) => {

    //解构
    const {
      advTitle,
      advType,
      advOrder
    } = params
    // console.log(file);

    try {
      advModel.create({ advTitle, advType, advOrder, advHref: "/upload/" + file.advPic.newFilename, addTime: new Date().toLocaleString() })

      return res.send({
        code: 10000,
        message: "添加成功",
        data: null
      })
    } catch (e) {
      console.log("添加失败" + e.message);
      res.send({
        code: 10001,
        message: "添加失败",
        data: null
      })
    }




  })

})


module.exports = route;